<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2017/11/9  19:27
 */
namespace app\api\logic;
use app\api\BaseModel;
use app\api\controller\Msn;
use think\Db;

class Order extends  BaseModel{

    /**
     * 预订房间
     */
    public function create_room(){
        $db_room = db('hotel_room');
        $start_date=isset($this->requestData['start_date'])?$this->requestData['start_date']:date('Y-m-d');
        $end_date=isset($this->requestData['end_date'])?$this->requestData['end_date']:date('Y-m-d',strtotime("+1 day"));
        if(strtotime($start_date)<strtotime(date("y-m-d"))){
            return array('message'=>'该商品已过期');
        }
        $where['r.status']=1;
        $where['r.delete']=1;
        $where['r.id']=$this->requestData['id'];
        $count=$this->requestData['count'];
        $realname=$this->requestData['realname'];
        $mobile=$this->requestData['mobile'];
         $book=(strtotime($end_date)-strtotime($start_date))/(24*60*60);
        $eachprice=0;
        for($i=0;$i<$book;$i++) {
            $where['p.roomdate'] = strtotime($start_date) + $i * 24 * 60 * 60;
            $list = $db_room->alias("r")
                ->join("hotel_room_price p", "p.roomid=r.id")
                ->field("r.id,r.title,r.type,r.image,r.des,p.oprice,p.roomid,p.roomdate,p.price1,p.price2,p.price3,p.price4,p.num")
                ->where($where)
                ->find();
            if (!$list) return array('记录不存在');
            if ($list['num'] < 1) return array('message' => date("Y-m-d", $where['p.roomdate']) . $list['title'] . '剩余数量为0，无法下单');
            if ($list['num'] < $count) return array('message' => date("Y-m-d", $where['p.roomdate']) . $list['title'] . '剩余数量为' . $list['num'] . '，不足' . $count);
            $level = $this->user['type'];
            switch ($level) {
                case 1:
                    $list['price'] = $list['price1'];
                    break;
                case 2:
                    $list['price'] = $list['price2'];
                    break;
                case 3:
                    $list['price'] = $list['price3'];
                    break;
                case 4:
                    $list['price'] = $list['price4'];
                    break;
            }
            $eachprice+=$list['price'];
        }
        $price=$eachprice*$count;
        $credit=0;
        if(isset($this->requestData['credit']) && $this->requestData['credit']!=''){
            $member=db('member')->where("id=".$this->user['id'])->field("integral")->find();
            if($member['integral']<$this->requestData['credit']) return array('message'=>'积分不足');
            $credit=$this->requestData['credit'];
            $config= db("config")->where("id=9")->find();
            $dicredit=floatval($config['value'])*$credit;
            $price=$price-$dicredit;
        }
        $cart_id=0;
        $code=0;//优惠券的code
        $couponmoney=0;//优惠券抵扣的金额
        if(isset($this->requestData['couponid']) && $this->requestData['couponid']!=''){
            $esa['card_id']=$this->requestData['couponid'];
            $find = db('coupon')->where($esa)->find();
            if(!$find) return array('message'=>'优惠券不存在');
            $week = $find['week'];
            $time = date('w');
            if($time==0){$date = '星期日';}
            if($time==1){$date = '星期一';}
            if($time==2){$date = '星期二';}
            if($time==3){$date = '星期三';}
            if($time==4){$date = '星期四';}
            if($time==5){$date = '星期五';}
            if($time==6){$date = '星期六';}
            if(!strstr($week,$date)) return array('message'=>'该优惠券只能在'.$week.'使用');
            $price=$price-$find['money'];
            $couponmoney=$find['money'];
            $cart_id=$esa['card_id'];
            $code= $this->requestData['code'];
            $order_code=db("order")->where("status !=-1 and code='$code'")->find();
            if($order_code) return array('message'=>'其他订单已使用该优惠券');
        }
        if(!cache("create")){
            cache("create",time());
        }else{
            if(time()-cache("create")<5){
                return array('message'=>'下单过于频繁，稍后再试');
            }else{
                cache("create",time());
            }
        }
        if($price<=0){
            $price=0.01;
        }

        $data=array(
            "credit"=>$credit,
            "order_sn"=>$this->build_order_no(),
            "price"=>$price,
            "uid"=>$this->user['id'],
            "create_time"=>time(),
            "type"=>0,
            "count"=>$count,
            "start_time"=>strtotime($start_date),
            "end_time"=>strtotime($end_date),
            "realname"=>serialize($realname),
            "mobile"=>$mobile,
            "title"=>$list['title'],
            "image"=>$list['image'],
            "code"=>$code,
            "couponmoney"=>$couponmoney,
            "cart_id"=>$cart_id,
            "goodsid"=>$this->requestData['id']
        );
        if(isset($this->requestData['remark']) && $this->requestData['remark']!=''){
            $data['remark']=$this->requestData['remark'];
        }
        Db::startTrans();
        try{
         $res=db("order")->insertGetId($data);
       if(!$res)   throw new \Exception('下单失败');
        $hotel_room_price=db("hotel_room_price");
        for($i=0;$i<$book;$i++) {
            $pri['roomdate'] = strtotime($start_date) + $i * 24 * 60 * 60;
            $pri['roomid'] = $this->requestData['id'];
            $sed= $hotel_room_price
                ->where($pri)
                ->setDec("num",$count);
            if(!$sed)   throw new \Exception('减库存失败');
        }
            if(isset($this->requestData['credit']) && $this->requestData['credit']!='') {
                $sed= db("member")->where("id=" . $this->user['id'])->setDec("integral",$this->requestData['credit']);
                if(!$sed)   throw new \Exception('减积分失败');
                $credit=array(
                    "uid"=>$this->user['id'],
                    "type"=>0,
                    "credit"=>$this->requestData['credit'],
                    "create_time"=>NOW_TIME,
                );
                $sed= db("credit_log")->insert($credit);
                if(!$sed)   throw new \Exception('增加积分消费记录失败');
            }
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return array("message"=>$e->getMessage());
        }
        return array("id"=>$res);
    }


    /**
     * 预订门票
     */
    public function create_ticket(){
        $db_room = db('hotel_room');
        $start_date=isset($this->requestData['start_date'])?$this->requestData['start_date']:date("Y-m-d");
        $end_date=isset($this->requestData['end_date'])?$this->requestData['end_date']:date('Y-m-d',strtotime("+1 day"));
        if($start_date!=date("Y-m-d")){
            return array('message'=>'该商品已过期');
        }
        $where['r.status']=1;
        $where['r.delete']=1;
        $where['r.id']=$this->requestData['id'];
        $book=(strtotime($end_date)-strtotime($start_date))/(24*60*60);
        $price=0;
        for($i=0;$i<$book;$i++) {
            $where['p.roomdate'] = strtotime($start_date) + $i * 24 * 60 * 60;
            $list = $db_room->alias("r")
                ->join("hotel_room_price p", "p.roomid=r.id")
                ->field("r.id,r.title,r.type,r.image,r.des,p.oprice,p.roomid,p.roomdate,p.price1,p.price2,p.price3,p.price4,p.num")
                ->where($where)
                ->find();
            if (!$list) return array('记录不存在');
            if ($list['num'] < 1) return array('message' => date("Y-m-d", $where['p.roomdate']) . $list['title'] . '剩余数量为0，无法下单');
            $level = $this->user['type'];
            switch ($level) {
                case 1:
                    $list['price'] = $list['price1'];
                    break;
                case 2:
                    $list['price'] = $list['price2'];
                    break;
                case 3:
                    $list['price'] = $list['price3'];
                    break;
                case 4:
                    $list['price'] = $list['price4'];
                    break;
            }
            $price+=$list['price'];
        }
        $credit=0;
        if(isset($this->requestData['credit']) && $this->requestData['credit']!=''){
            $member=db('member')->where("id=".$this->user['id'])->field("integral")->find();
            if($member['integral']<$this->requestData['credit']) return array('message'=>'积分不足');
            $credit=$this->requestData['credit'];
            $config= db("config")->where("id=9")->find();
            $dicredit=floatval($config['value'])*$credit;
            $price=$price-$dicredit;

        }
        $cart_id=0;
        $code=0;//优惠券的code
        $couponmoney=0;//优惠券抵扣的金额
        if(isset($this->requestData['couponid']) && $this->requestData['couponid']!=''){
            $esa['card_id']=$this->requestData['couponid'];
            $find = db('coupon')->where($esa)->find();
            if(!$find) return array('message'=>'优惠券不存在');
            $week = $find['week'];
            $time = date('w');
            if($time==0){$date = '星期日';}
            if($time==1){$date = '星期一';}
            if($time==2){$date = '星期二';}
            if($time==3){$date = '星期三';}
            if($time==4){$date = '星期四';}
            if($time==5){$date = '星期五';}
            if($time==6){$date = '星期六';}
            if(!strstr($week,$date)) return array('message'=>'该优惠券只能在'.$week.'使用');
            $price=$price-$find['money'];
            $couponmoney=$find['money'];
            $cart_id=$esa['card_id'];
            $code= $this->requestData['code'];
            $order_code=db("order")->where("status !=-1 and code='$code'")->find();
            if($order_code) return array('message'=>'其他订单已使用该优惠券');
        }
        if(!cache("create")){
            cache("create",time());
        }else{
            if(time()-cache("create")<5){
                return array('message'=>'下单过于频繁，稍后再试');
            }else{
                cache("create",time());
            }
        }
        if($price<=0){
            $price=0.01;
        }
        $data=array(
            "credit"=>$credit,
            "order_sn"=>$this->build_order_no(),
            "price"=>$price,
            "uid"=>$this->user['id'],
            "create_time"=>time(),
            "type"=>1,
            "count"=>1,
            "start_time"=>strtotime($start_date),
            "end_time"=>strtotime($end_date),
            "title"=>$list['title'],
            "image"=>$list['image'],
            "code"=>$code,
            "couponmoney"=>$couponmoney,
            "cart_id"=>$cart_id,
            "goodsid"=>$this->requestData['id']
        );
        if(isset($this->requestData['remark']) && $this->requestData['remark']!=''){
            $data['remark']=$this->requestData['remark'];
        }
        Db::startTrans();
        try{
            $res=db("order")->insertGetId($data);
            if(!$res)   throw new \Exception('下单失败');
            $hotel_room_price=db("hotel_room_price");
            for($i=0;$i<$book;$i++) {
                $pri['roomdate'] = strtotime($start_date) + $i * 24 * 60 * 60;
                $pri['roomid'] = $this->requestData['id'];
                $sed= $hotel_room_price
                    ->where($pri)
                    ->setDec("num");
                if(!$sed)   throw new \Exception('减库存失败');
            }
            if(isset($this->requestData['credit']) && $this->requestData['credit']!='') {
                $sed= db("member")->where("id=" . $this->user['id'])->setDec("integral",$this->requestData['credit']);
                if(!$sed)   throw new \Exception('减积分失败');
                $credit=array(
                    "uid"=>$this->user['id'],
                    "type"=>0,
                    "credit"=>$this->requestData['credit'],
                    "create_time"=>NOW_TIME,
                );
                $sed= db("credit_log")->insert($credit);
                if(!$sed)   throw new \Exception('增加积分消费记录失败');
            }
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return array("message"=>$e->getMessage());
        }
        return array("id"=>$res);
    }
    /**
     * 生成唯一订单号
     */
    public function build_order_no()
    {
        $no = date('YmdHis') .rand(1000,9999);
        //检测是否存在
        $info = db('order')->where(array('order_sn' => $no))->find();
        (!empty($info)) && $no = $this->build_order_no();
        return $no;
    }
    /**
     * 购买套票
     */
    public function create_package(){
        $db_package = db('package');
        $db_room=db('hotel_room');
        $db_order_package=db('order_package');
        $where['status']=1;
        $where['delete']=1;
        $where['id']=$this->requestData['id'];
        $package=$db_package->where($where)->find();
        if(!$package) return array('message'=>'套票不存在');
        if($package['start_date']<date('Y-m-d')) return array('message'=>'套票已过期');
        $realname='';
        $mobile='';
        if($package['type']==0) {
            $start_date = $package['start_date'];
            $end_date = $package['end_date'];
            $realname=serialize($this->requestData['realname']);
            $mobile=$this->requestData['mobile'];
        }else{
            $start_date = $package['start_date'];
            $end_date = $package['start_date']+24*60*60;
        }
        if($start_date<strtotime(date("y-m-d"))){
            return array('message'=>'该商品已过期');
        }
        $credit=0;
        $price=$package['price'];
        if(isset($this->requestData['credit']) && $this->requestData['credit']!=''){
            $member=db('member')->where("id=".$this->user['id'])->field("integral")->find();
            if($member['integral']<$this->requestData['credit']) return array('message'=>'积分不足');
            $credit=$this->requestData['credit'];
           $config= db("config")->where("id=9")->find();
            $dicredit=floatval($config['value'])*$credit;
            $price=$package['price']-$dicredit;
        }
        $cart_id=0;
        $code=0;//优惠券的code
        $couponmoney=0;//优惠券抵扣的金额
        if(isset($this->requestData['couponid']) && $this->requestData['couponid']!=''){
            $esa['card_id']=$this->requestData['couponid'];
            $find = db('coupon')->where($esa)->find();
            if(!$find) return array('message'=>'优惠券不存在');
            $week = $find['week'];
            $time = date('w');
            $date='';
            if($time==0){$date = '星期日';}
            if($time==1){$date = '星期一';}
            if($time==2){$date = '星期二';}
            if($time==3){$date = '星期三';}
            if($time==4){$date = '星期四';}
            if($time==5){$date = '星期五';}
            if($time==6){$date = '星期六';}
            if(!strstr($week,$date)) return array('message'=>'该优惠券只能在'.$week.'使用');
            $price=$price-$find['money'];
            $couponmoney=$find['money'];
            $cart_id=$esa['card_id'];
            $code= $this->requestData['code'];
            $order_code=db("order")->where("status !=-1 and code='$code'")->find();
            if($order_code) return array('message'=>'其他订单已使用该优惠券');
        }

         $option=unserialize($package['option']);
        $count=0;
        foreach($option as $v) {
            $op['r.status']=1;
            $op['r.delete']=1;
            $op['r.id']=$v['id'];
            $list = $db_room->alias("r")
                ->join("hotel_room_price p", "p.roomid=r.id")
                ->field("r.id,p.num,r.title,p.roomdate")
                ->where($op)
                ->find();
            if (!$list) return array('记录不存在');
            if ($list['num'] < 1) return array('message' => date("Y-m-d", $list['roomdate']) . $list['title'] . '剩余数量为0，无法下单');
            if ($list['num'] < $v['num']) return array('message' => date("Y-m-d", $list['roomdate']) . $list['title'] . '剩余数量为' . $list['num'] . '，不足' . $v['num']);
        $count+=$v['num'];
        }
        if($price<=0){
            $price=0.01;
        }
        $data=array(
            "credit"=>$credit,
            "order_sn"=>$this->build_order_no(),
            "price"=>$price,
            "uid"=>$this->user['id'],
            "create_time"=>time(),
            "type"=>2,
            "isroom"=>$package['type'],
            "count"=>$count,
            "start_time"=>$start_date,
            "end_time"=>$end_date,
            "realname"=>$realname,
            "mobile"=>$mobile,
            "title"=>$package['title'],
            "image"=>$package['image'],
            "code"=>$code,
            "couponmoney"=>$couponmoney,
            "cart_id"=>$cart_id,
        );
        if(isset($this->requestData['remark']) && $this->requestData['remark']!=''){
            $data['remark']=$this->requestData['remark'];
        }
    if(!cache("create")){
            cache("create",time());
        }else{
            if(time()-cache("create")<5){
                return array('message'=>'下单过于频繁，稍后再试');
            }else{
                cache("create",time());
            }
        }
    Db::startTrans();
        try{
            $order_id=db("order")->insertGetId($data);
            if(!$order_id)   throw new \Exception('下单失败');
        $hotel_room_price=db("hotel_room_price");
           foreach($option as $v) {
               $pri['roomdate'] = $start_date;
               $pri['roomid'] = $v['id'];

               $sed = $hotel_room_price
                   ->where($pri)
                   ->setDec("num", $v['num']);
               if (!$sed) throw new \Exception('减库存失败');
               $o_pack['order_id'] = $order_id;
               $o_pack['goodsid'] = $v['id'];
               $o_pack['num'] = $v['num'];
               $opid = $db_order_package->insertGetId($o_pack);
               if (!$opid) throw new \Exception('失败');
           }
            if(isset($this->requestData['credit']) && $this->requestData['credit']!='') {
                $sed= db("member")->where("id=" . $this->user['id'])->setDec("integral",$this->requestData['credit']);
                if(!$sed)   throw new \Exception('减积分失败');
                $credit=array(
                    "uid"=>$this->user['id'],
                    "type"=>0,
                    "credit"=>$this->requestData['credit'],
                    "create_time"=>NOW_TIME,
                );
                $sed= db("credit_log")->insert($credit);
                if(!$sed)   throw new \Exception('增加积分消费记录失败');
            }
      Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return array("message"=>$e->getMessage());
        }
        return array("id"=>$order_id);
    }

    /**
     * 订单列表
     */
    public function index()
    {
        $db_order=db("order");
        $p=isset($this->requestData['page'])?$this->requestData['page']:'1';
        $size=isset($this->requestData['limit'])?$this->requestData['limit']:'10';
        $where['delete']=0;
        if(isset($this->requestData['status']) && $this->requestData['status']!=''){
            $where['status'] = $this->requestData['status'];
        }else {
            $where['status'] = array("neq", "-1");
        }
        $where['uid']=$this->user['id'];
        $list=$db_order->where($where)
            //->whereOr($where)
            ->page($p,$size)
            ->order("id desc")
            ->select();

        foreach($list as &$v){
            if($v['status']==0 && NOW_TIME-$v['create_time']>1800){
                $db_order->where("id=".$v['id'])->update(array("status"=>-1));
            }
            if($v['status']==1 && NOW_TIME-$v['end_time']>=0){
                $db_order->where("id=".$v['id'])->update(array("status"=>2));
                $pre=floor($v['price']);
                db("member")->where("id=".$v['uid'])->setInc("integral",$pre);
            }
            if($v['status']==1 && NOW_TIME-$v['pay_time']>=2*60*60){
                $v['closerefund']=1;
            }else{
                $v['closerefund']=0;
            }
            $v['image']=IMG_PATH.$v['image'];
            $v['create_time']=date("Y-m-d H:i:s",$v['create_time']);
            $v['finish_time']=!empty($v['finish_time'])?date("Y-m-d H:i:s",$v['finish_time']):$v['finish_time'];
            $v['start_time']=date("Y-m-d",$v['start_time']);
            $v['end_time']=date("Y-m-d",$v['end_time']);
        }
        $count=$db_order->where($where)->count();
        return array(
            "list"=>$list,
            "page_num"=>$p,
            "page_limit"=>$size,
            "count"=>$count,
            );
    }
    /**
     * 订单详情
     */
    public function detail()
    {
        $db_order = db('order');
        $where['delete']=0;
        $where['id']=$this->requestData['id'];
        $where['uid']=$this->user['id'];
        $list=$db_order->where($where)
            ->order("id desc")
            ->find();
        if(!$list) return array('message'=>'订单不存在');
        $list['day_num']=($list['end_time']-$list['start_time'])/(24*60*60);
        if($list['realname']){
            $list['realname']=unserialize($list['realname']);
        }
        $list['image']=IMG_PATH. $list['image'];
        $list['create_time']=date("Y-m-d H:i:s",$list['create_time']);
        $list['finish_time']=!empty($list['finish_time'])?date("Y-m-d H:i:s",$list['finish_time']):$list['finish_time'];
        $list['start_time']=date("Y-m-d",$list['start_time']);
        $list['end_time']=date("Y-m-d",$list['end_time']);
        return $list;
    }
    /**
     * 取消订单
     */
    public function cancel()
    {
        $db_order = db('order');
        $where['delete']=0;
        $where['id']=$this->requestData['id'];
        $where['uid']=$this->user['id'];
        $list=$db_order->where($where)->find();
        if(!$list) return array('message'=>'订单不存在');
        if($list['status']!=0) return array('message'=>'不是待支付状态，不能取消');
        $res=$db_order->where($where)->update(array('status'=>-1));
        if(!$res) return array('message'=>'系统繁忙，稍后再试');
        $member=db("member")->where("id=".$list['uid'])->find();
        $app=(new Pay())->app();
        if($list['type']==0){
            $data=array(
                'touser' => $member['openid'],
                'template_id' => 'A6KOtuFCkEhu4rxb5in4zb2mDWs-abrPVyxh-_v6QA0',
                'url' =>  request()->domain().'/qchwap/#/orderDetail?id='.$list['id'],
                'data' => [
                    'first' => '订单取消成功通知',
                    'keyword1' => $list['order_sn'],
                    'keyword2'=>'新兴县七彩虹酒店',
                    'keyword3' =>date("Y-m-d H:i",$list['start_time']),
                    'keyword4' =>date("Y-m-d H:i",$list['end_time']),
                    'keyword5' =>$list['title'],
                    'remark' => '如有疑问请致电酒店：0766-6636678'
                ]
            );
            $app->notice->send($data);
        }
        return array();
    }
    /**
     * 确认入住
     */
    public function check()
    {
        $db_order = db('order');
        $where['delete']=0;
        $where['id']=$this->requestData['id'];
        $where['uid']=$this->user['id'];
        $list=$db_order->where($where)->find();
        if(!$list) return array('message'=>'订单不存在');
        if($list['status']!=1) return array('message'=>'不是待入住状态');
        $res=$db_order->where($where)->update(array('ischeck'=>1,'check_time'=>NOW_TIME));
        if(!$res) return array('message'=>'系统繁忙，稍后再试');
        if($list['type']==0){
            $member=db("member")->where("id=".$list['uid'])->find();
            $app=(new Pay())->app();
            $data=array(
                'touser' => $member['openid'],
                'template_id' => 'n_d1ByJAoeVtky7YX9VzJTMY9Kf1K_-bxk87Szp35n4',
                'url' =>  request()->domain().'/qchwap/#/orderDetail?id='.$list['id'],
                'data' => [
                    'first' => '入住成功',
                    'hotelName' => '七彩虹酒店',
                    'roomName'=>$list['title'],
                    'date' => date("Y-m-d H:i"),
                    'remark' => '如有疑问请致电酒店：0766-6636678'
                ]
            );
            $app->notice->send($data);
        }

        return array();
    }
    /**
     * 删除订单
     */
    public function delete()
    {
        $db_order = db('order');
        $where['delete']=0;
        $where['id']=$this->requestData['id'];
        $where['uid']=$this->user['id'];
        $list=$db_order->where($where)->find();
        if(!$list) return array('message'=>'订单不存在');
        if(!in_array($list['status'],array(2,3))) return array('message'=>'不是完成状态，不能删除');
        $res=$db_order->where($where)->update(array('delete'=>-1));
        if(!$res) return array('message'=>'系统繁忙，稍后再试');
        return array();
    }


    /**
     * 申请退款
     */
    public function refund()
    {
        $db_order = db('order');
        $db_redund = db('order_refund');
        $where['status'] = 1;
        $where['delete'] = 0;
        $where['uid'] =$this->user['id'];
        $where['id'] = $this->requestData['id'];
        $order = $db_order->where($where)->find();
        if (!$order) return array('message' => '订单不存在');
        if(NOW_TIME-$order['pay_time']>=2*60*60) return array('message' => '支付超过两小时，不能退款');
        if($order['price']<$this->requestData['money']) return array('message' => '申请金额大于订单金额');
        $data=array(
            "uid"=>$this->user['id'],
            "order_id"=>$this->requestData['id'],
            "reason"=>$this->requestData['reason'],
            "money"=>$this->requestData['money'],
            "des"=>isset($this->requestData['des'])?$this->requestData['des']:'',
            "create_time"=>NOW_TIME,
        );
        Db::startTrans();
        try{
             $res=$db_redund->insertGetId($data);
            if(!$res) throw new \Exception('申请失败');
            $deal = $db_order->where($where)->update(array('status'=>-2));
            if(!$res) throw new \Exception('更改订单状态失败');
            Db::commit();
        } catch (\Exception $e) {// 回滚事务
            Db::rollback();
            return array("message"=>$e->getMessage());
        }
        return array();
    }

    public function qrcode()
    {
        vendor("phpqrcode.phpqrcode");
        $db_order = db('order');
        $where['status'] = 1;
        $where['delete'] = 0;
        $where['uid'] =$this->user['id'];
        $where['id'] = $this->requestData['id'];
        $order = $db_order->where($where)->find();
        if (!$order) return array('message' => '订单不存在');
        $url= request()->domain().'/qrcode/'.$where['id'].'.png';
        if(file_exists(QRCODE_PATH.$where['id'].'.png')){
            return array("title"=>$order['title'],"url"=>$url,"order_sn"=>$order['order_sn']);
        }
        $value =$order['type'].$where['id']; //二维码内容
        $errorCorrectionLevel = 'H';//容错级别
        $matrixPointSize = 6;//生成图片大小
        //生成二维码图片
        \QRcode::png($value,QRCODE_PATH. $where['id'].'.png', $errorCorrectionLevel, $matrixPointSize, 2);
       $logo = ROOT_PATH.'public/logo.jpg';//准备好的logo图片
        $QR =QRCODE_PATH.$where['id'].'.png';//已经生成的原始二维码图
        if ($logo !== FALSE) {
            $QR = imagecreatefromstring(file_get_contents($QR));
            $logo = imagecreatefromstring(file_get_contents($logo));
            $QR_width = imagesx($QR);//二维码图片宽度
            $QR_height = imagesy($QR);//二维码图片高度
            $logo_width = imagesx($logo);//logo图片宽度
            $logo_height = imagesy($logo);//logo图片高度
            $logo_qr_width = $QR_width / 5;
            $scale = $logo_width/$logo_qr_width;
            $logo_qr_height = $logo_height/$scale;
            $from_width = ($QR_width - $logo_qr_width) / 2;
            //重新组合图片并调整大小
            imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
                $logo_qr_height, $logo_width, $logo_height);
        }
        ImagePng($QR,QRCODE_PATH.$where['id'].'.png');
        if(file_exists(QRCODE_PATH.$where['id'].'.png')){
            return array("title"=>$order['title'],"url"=>$url,"order_sn"=>$order['order_sn']);
        }
    }

}